class UsersController < ApplicationController
  skip_before_action :logged_in_user,only: [:new, :create]
  before_action :session_login,except: [:edit, :update]
  def show
    @user = User.find(params[:id])
  end


  def new
    @user = User.new
    render 'new', layout: 'login'
  end

  def create
    @user = User.new(user_params)
    if @user.save
     @pc_campaign =  Campaign.create(user_id: @user.id,name: 'pc计划', campaign_type: 1,campaign_status: 1 )
     @mobile_campaign = Campaign.create(user_id: @user.id,name: 'mobile计划', campaign_type: 2,campaign_status: 1 )
     @pc_campaign.save
     @mobile_campaign.save
      log_in @user
      flash[:notice] = "登录成功"
      redirect_to root_url, layout: 'login'
    else
      #flash[:danger] = "Please check your email to activate your account."
      render 'new'
    end
  end

  def edit
      @user = @current_user
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      # 处理更新成功的情况
      flash[:success] = "更新成功"
      redirect_to edit_user_path
    else
      render 'edit'
    end
  end

  private
  def user_params
    params.require(:user).permit(:name, :username, :email, :password, :password_confirmation)
  end


end
